package com.gy.algorithm.carl.hashtableexa;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @BelongsProject: javabase
 * @BelongsPackage: com.gy.algorithm.carl
 * @Author: gy
 * @Date: 2024/10/22 11:30
 * @Description:
 */
public class IsAnagram242 {

    public static void main(String[] args) {

    }

    // end1
    public boolean isAnagram1(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }
        char[] str1 = s.toCharArray();
        char[] str2 = t.toCharArray();
        Arrays.sort(str1);
        Arrays.sort(str2);
        return Arrays.equals(str1, str2);
    }


    // write
    public static boolean isAnagram(String s, String t) {
        if (s.length() != t.length()) {
            return false;
        }

         int[] records = new int[26];

        for (int i = 0; i < s.length(); i++) {
            records[s.charAt(i) - 'a'] ++;
        }

        for (int i = 0; i < t.length(); i++) {
            records[t.charAt(i) - 'a'] --;
            if(records[t.charAt(i) - 'a'] < 0){
                return false;
            }
        }

        return true;
    }
}
